1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.ShellUtils;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Str;
30 private import glib.c.functions;
31 public  import glib.c.types;
32 
33 
34 /** */
35 public struct ShellUtils
36 {
37 
38 	/** */
39 	public static GQuark shellErrorQuark()
40 	{
41 		return g_shell_error_quark();
42 	}
43 
44 	/**
45 	 * Parses a command line into an argument vector, in much the same way
46 	 * the shell would, but without many of the expansions the shell would
47 	 * perform (variable expansion, globs, operators, filename expansion,
48 	 * etc. are not supported).
49 	 *
50 	 * The results are defined to be the same as those you would get from
51 	 * a UNIX98 `/bin/sh`, as long as the input contains none of the
52 	 * unsupported shell expansions. If the input does contain such expansions,
53 	 * they are passed through literally.
54 	 *
55 	 * Possible errors are those from the %G_SHELL_ERROR domain.
56 	 *
57 	 * In particular, if @command_line is an empty string (or a string containing
58 	 * only whitespace), %G_SHELL_ERROR_EMPTY_STRING will be returned. It’s
59 	 * guaranteed that @argvp will be a non-empty array if this function returns
60 	 * successfully.
61 	 *
62 	 * Free the returned vector with g_strfreev().
63 	 *
64 	 * Params:
65 	 *     commandLine = command line to parse
66 	 *     argvp = return location for array of args
67 	 *
68 	 * Returns: %TRUE on success, %FALSE if error set
69 	 *
70 	 * Throws: GException on failure.
71 	 */
72 	public static bool shellParseArgv(string commandLine, out string[] argvp)
73 	{
74 		int argcp;
75 		char** outargvp = null;
76 		GError* err = null;
77 
78 		auto __p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &outargvp, &err) != 0;
79 
80 		if (err !is null)
81 		{
82 			throw new GException( new ErrorG(err) );
83 		}
84 
85 		argvp = Str.toStringArray(outargvp, argcp);
86 
87 		return __p;
88 	}
89 
90 	/**
91 	 * Quotes a string so that the shell (/bin/sh) will interpret the
92 	 * quoted string to mean @unquoted_string.
93 	 *
94 	 * If you pass a filename to the shell, for example, you should first
95 	 * quote it with this function.
96 	 *
97 	 * The return value must be freed with g_free().
98 	 *
99 	 * The quoting style used is undefined (single or double quotes may be
100 	 * used).
101 	 *
102 	 * Params:
103 	 *     unquotedString = a literal string
104 	 *
105 	 * Returns: quoted string
106 	 */
107 	public static string shellQuote(string unquotedString)
108 	{
109 		auto retStr = g_shell_quote(Str.toStringz(unquotedString));
110 
111 		scope(exit) Str.freeString(retStr);
112 		return Str.toString(retStr);
113 	}
114 
115 	/**
116 	 * Unquotes a string as the shell (/bin/sh) would.
117 	 *
118 	 * This function only handles quotes; if a string contains file globs,
119 	 * arithmetic operators, variables, backticks, redirections, or other
120 	 * special-to-the-shell features, the result will be different from the
121 	 * result a real shell would produce (the variables, backticks, etc.
122 	 * will be passed through literally instead of being expanded).
123 	 *
124 	 * This function is guaranteed to succeed if applied to the result of
125 	 * g_shell_quote(). If it fails, it returns %NULL and sets the
126 	 * error.
127 	 *
128 	 * The @quoted_string need not actually contain quoted or escaped text;
129 	 * g_shell_unquote() simply goes through the string and unquotes/unescapes
130 	 * anything that the shell would. Both single and double quotes are
131 	 * handled, as are escapes including escaped newlines.
132 	 *
133 	 * The return value must be freed with g_free().
134 	 *
135 	 * Possible errors are in the %G_SHELL_ERROR domain.
136 	 *
137 	 * Shell quoting rules are a bit strange. Single quotes preserve the
138 	 * literal string exactly. escape sequences are not allowed; not even
139 	 * `\'` - if you want a `'` in the quoted text, you have to do something
140 	 * like `'foo'\''bar'`. Double quotes allow `$`, ```, `"`, `\`, and
141 	 * newline to be escaped with backslash. Otherwise double quotes
142 	 * preserve things literally.
143 	 *
144 	 * Params:
145 	 *     quotedString = shell-quoted string
146 	 *
147 	 * Returns: an unquoted string
148 	 *
149 	 * Throws: GException on failure.
150 	 */
151 	public static string shellUnquote(string quotedString)
152 	{
153 		GError* err = null;
154 
155 		auto retStr = g_shell_unquote(Str.toStringz(quotedString), &err);
156 
157 		if (err !is null)
158 		{
159 			throw new GException( new ErrorG(err) );
160 		}
161 
162 		scope(exit) Str.freeString(retStr);
163 		return Str.toString(retStr);
164 	}
165 }